package com.jintian.smart.kernel.core.web;

import com.jintian.smart.kernel.core.constant.SmartConstant;
import lombok.Data;

import java.io.Serializable;

/**
 * code\message使用xxx.properties
 */
@Data
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private int code;
    private String message;
    private long time;
    private boolean success;
    private T data;

    private Result() {
        this.time = System.currentTimeMillis();
    }

    private Result(int resultCode, String msg) {
        this(resultCode, null, msg);
    }

    private Result(int code, T data, String message) {
        this.time = System.currentTimeMillis();
        this.code = code;
        this.data = data;
        this.message = message;
        this.time = System.currentTimeMillis();
        this.success = ResultCode.SUCCESS.code == code;
    }

    public static <T> Result<T> data(T data) {
        return data(data, null);
    }

    public static <T> Result<T> data(T data, String message) {
        return data(ResultCode.SUCCESS.code, data, message);
    }

    public static <T> Result<T> data(int code, T data, String message) {
        return new Result<>(code, data, message);
    }

    public static <T> Result<T> success(String msg) {
        return new Result<>(ResultCode.SUCCESS.code, msg);
    }

    public static <T> Result<T> fail(String msg) {
        return new Result<>(ResultCode.ERROR.code, msg);
    }

    public static <T> Result<T> fail() {
        return new Result<>(ResultCode.ERROR.code, "系统内部错误");
    }

    public static <T> Result<T> condition(boolean flag) {
        return flag ? success(SmartConstant.DEFAULT_SUCCESS_MESSAGE) : fail(SmartConstant.DEFAULT_FAIL_MESSAGE);
    }
}
